package com.scanner.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.scanner.bean.ScanSetting;
import com.scanner.hardware.R;
import com.scanner.util.BaseUtils;


public class ChooseCloseScanType extends DialogFragment {
    private String value;
    private String scanModel;
    private ScanSetting settingFormShare;

    public static ChooseCloseScanType getInstance(String type){
        ChooseCloseScanType scanType = new ChooseCloseScanType();
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        scanType.setArguments(bundle);
        return scanType;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if(arguments != null){
            value = arguments.getString("type");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomProgressDialog);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return inflater.inflate(R.layout.dialog_choose_closetype_layout,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RadioGroup rg = view.findViewById(R.id.rg_select);
        RadioButton rbAync = view.findViewById(R.id.rb_Async);
        RadioButton rbHor = view.findViewById(R.id.rb_sync);

        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnSure = view.findViewById(R.id.btn_update);
        settingFormShare = BaseUtils.getSettingFormShare();
        scanModel = settingFormShare.getScanModel();
        if("Async".equalsIgnoreCase(scanModel)){//松手后关闭
            rbAync.setChecked(true);
        }else if("Sync".equalsIgnoreCase(scanModel)){//得到结果后关闭
            rbHor.setChecked(true);
        }else {
            rbAync.setChecked(true);
        }

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_sync){
                    scanModel = "Sync";

                }else if(checkedId == R.id.rb_Async){
                    scanModel = "Async";
                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissDialog();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settingFormShare.setScanModel(scanModel);
                BaseUtils.beanToString(settingFormShare);
                dissDialog();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        Window dialogWindow = getDialog().getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);

    }

    public void dissDialog(){
        if(getDialog()!= null && getDialog().isShowing()){
            getDialog().dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}
